<template>
  <div class="notice-container">
    <div class="notice-header">
      <h1 class="notice-title">hs-AntMoves蚂蚁搬家</h1>
      <div class="search-box">
        <el-input
            v-model="keywords"
            placeholder="关键字查询公告"
            class="search-input"
        >
          <template #append>
            <el-button type="primary" @click="load">查找</el-button>
          </template>
        </el-input>
      </div>
    </div>

    <div class="notice-list">
      <div v-for="item in noticeList" :key="item.id" class="notice-item" @click="goDetail(item.id)">
        <div class="notice-image">
          <el-image :src="item.banner_img" fit="cover" />
        </div>
        <div class="notice-content">
          <h3 class="notice-title">{{ item.title }}</h3>
          <p class="notice-desc">{{ item.info }}</p>
          <p class="notice-time">{{dayjs(item.create_time).format('YYYY-MM-DD HH:mm:ss')}}</p>
        </div>
      </div>
    </div>

    <div class="pagination">
      <el-pagination :total=total v-model:current-page="pagination.currentPage" :page-size=pagination.pageSize @current-change="load" />

      <span class="page-info">共 {{ total }} 条 10条/页</span>
    </div>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue'
import {get, takeAccessToken} from "@/net/index.js";
import {ElMessage} from "element-plus";
import dayjs from "dayjs";
import router from "@/router/index.js";

const keywords = ref('')
const total = ref()
const noticeList = ref([])
const pagination = reactive({
  pageSize: 3,
  currentPage: 1,
})
const goDetail = (id) => {
  router.push(`/user/user_notice/${id}`)
}

const handleNoticeClick = (notice) => {
  // 处理点击公告的逻辑
  console.log('点击公告:', notice)
}


const load = () => {
  console.log(keywords.value)

  get(`/api/notice/getNoticeList/${pagination.currentPage}?keywords=${keywords.value || ''}`,(res)=>{
    total.value = res.total
    noticeList.value = res.list
    console.log(res)
  },(err)=>{
    ElMessage.warning("请求失败")
    console.log(err)
  })

}

onMounted(() => {
  load()

})
</script>

<style scoped>
.notice-container {
  max-width: 1150px;
  margin: 30px auto;
}

.notice-header {
  margin-bottom: 30px;
}

.notice-header .notice-title {
  font-size: 32px;
  color: #333;
  margin-bottom: 20px;
}

.search-box {
  width: 100%;
}

.search-input {
  width: 50%;
}

.notice-list {
  margin-bottom: 30px;
}

.notice-item {
  display: flex;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
  transition: all 0.3s ease;
}

.notice-item:hover {
  transform: translateX(10px);
  background-color: #f9f9f9;
}

.notice-image {
  width: 200px;
  height: 140px;
  margin-right: 20px;
  border-radius: 4px;
  overflow: hidden;
  flex-shrink: 0;
}

.notice-image :deep(.el-image) {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.notice-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.notice-content .notice-title {
  font-size: 18px;
  color: #333;
  margin: 0 0 12px;
  cursor: pointer;
}

.notice-content .notice-title:hover {
  color: #409EFF;
}

.notice-desc {
  color: #666;
  font-size: 14px;
  line-height: 1.6;
  margin: 0 0 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.notice-time {
  color: #999;
  font-size: 14px;
  margin: 0;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
}

.page-info {
  color: #666;
  font-size: 14px;
}
</style>